<?php
/**
 * Created by PhpStorm.
 * User: yu
 * Date: 2017/1/6
 * Time: 下午4:19
 */

namespace Admin\Controller;


use Common\Controller\AdminController;
use Common\Model\CategoryModel;
use Common\Model\TypeModel;
use Org\Data\Data;

class CategoryController extends AdminController
{
	protected $categoryModel;
	public function __init(){
		$this->categoryModel = new CategoryModel();
	}
	public function lists(){
		$cateList = $this->categoryModel->select();
		$cateTree = (new Data())->tree($cateList,'cname');

		$this->assign('cateTree',$cateTree);
		$this->display();
	}
	public function addTop(){
		if(IS_POST){
			$post['cname'] = I('post.cname');
			$post['pid'] = 0;
			$this->store($this->categoryModel,$post,function(){
				$this->success('操作成功',u('admin/category/lists'));
			});
		}else{
			$this->display();
		}
	}

	public function addSon(){
		$pid = I('get.pid');
		if(IS_POST){
			$this->store($this->categoryModel,I('post.'),function(){
				$this->success('操作成功',u('admin/category/lists'));
			});
		}else{
			//分配类型数据
			$typeList = (new TypeModel())->select();
			$this->assign('typeList',$typeList);
			$top = $this->categoryModel->find($pid);
			$this->assign('top',$top);
			$this->display();
		}
	}
}